package cn.hftoday.org;

import java.util.*;
import junit.framework.TestCase;
import cn.hftoday.db.*;
import cn.hftoday.core.*;
import cn.hftoday.util.*;

public class CompanyTest extends TestCase {

	protected void setUp() throws Exception {
		ApplicationConfig.getInstance().load();
		ConnectionPool.getInstance().open();
		ConnectionPool.getInstance().beginTransaction();
	}

	protected void tearDown() throws Exception {
		ConnectionPool.getInstance().rollback();
		ConnectionPool.getInstance().close();
	}

	public void testGetAlias() {
		Company c = new Company();
		c.setAlias("abcdefgh");
		assertEquals(c.getAlias(), "abcdefgh");
	}

	public void testGetCity() {
		Company c = new Company();
		c.setCity("abcdefgh");
		assertEquals(c.getCity(), "abcdefgh");
	}
	
	public void testGetAddress() {
		Company c = new Company();
		c.setAddress("abcdefgh");
		assertEquals(c.getAddress(), "abcdefgh");
	}

	public void testGetTaxNo() {
		Company c = new Company();
		c.setTaxNo("abcdefgh");
		assertEquals(c.getTaxNo(), "abcdefgh");
	}

	public void testGetFoundTime() {
		Company c = new Company();
		c.setFoundTime(CalendarParser.toCalendar("2009-01-01 01:09:54"));
		assertEquals(c.getFoundTime(), CalendarParser.toCalendar("2009-01-01 01:09:54"));
	}

	public void testGetScale() {
		Company c = new Company();
		c.setScale("abcdefgh");
		assertEquals(c.getScale(), "abcdefgh");
	}
	
	public void testGetIndustry() {
		Company c = new Company();
		c.setIndustry("abcdefgh");
		assertEquals(c.getIndustry(), "abcdefgh");
	}
	
	public void testSelect() {
		testInsert();
		
		Company c = (Company)Mapper.getInstance().queryForObject("SelectCompanyById", "1234098834567654uio");
		assertEquals(c.getId(), "1234098834567654uio");
		assertEquals(c.getName(), "ms-oracle");
		assertEquals(c.isHidden(), true);
		assertEquals(c.getCreateTime(), CalendarParser.toCalendar("2009-01-01 01:09:54"));
		assertEquals(c.getUpdateTime(), CalendarParser.toCalendar("2009-01-01 01:09:55"));
		assertEquals(c.getAlias(), "bea");
		assertEquals(c.getCity(), "ny");
		assertEquals(c.getAddress(), "whitehouse");
		assertEquals(c.getTaxNo(), "2345678");
		assertEquals(c.getFoundTime(), CalendarParser.toCalendar("2009-01-01 01:09:56"));
		assertEquals(c.getScale(), "20-30");
		assertEquals(c.getIndustry(), "it");
	}
	
	public void testInsert() {
		Company c = new Company();
		c.setId("1234098834567654uio");
		c.setName("ms-oracle");
		c.setHidden(true);
		c.setCreateTime(CalendarParser.toCalendar("2009-01-01 01:09:54"));
		c.setUpdateTime(CalendarParser.toCalendar("2009-01-01 01:09:55"));
		c.setAlias("bea");
		c.setCity("ny");
		c.setAddress("whitehouse");
		c.setTaxNo("2345678");
		c.setFoundTime(CalendarParser.toCalendar("2009-01-01 01:09:56"));
		c.setScale("20-30");
		c.setIndustry("it");
		Object o = Mapper.getInstance().insert("InsertCompany", c);
		assertSame(c, o);
	}
	
	public void testUpdate() {
		testInsert();
		
		Company c = (Company)Mapper.getInstance().queryForObject("SelectCompanyById", "1234098834567654uio");
		c.setName("ms-oraclee");
		c.setHidden(false);
		c.setCreateTime(CalendarParser.toCalendar("2009-01-01 01:10:54"));
		c.setUpdateTime(CalendarParser.toCalendar("2009-01-01 01:10:55"));
		c.setAlias("bea2");
		c.setCity("nyork");
		c.setAddress("the whitehouse");
		c.setTaxNo("234567800");
		c.setFoundTime(CalendarParser.toCalendar("2009-01-10 01:09:56"));
		c.setScale("20-50");
		c.setIndustry("IT");
		Mapper.getInstance().update("UpdateCompany", c);
		
		c = (Company)Mapper.getInstance().queryForObject("SelectCompanyById", "1234098834567654uio");
		assertEquals(c.getName(), "ms-oraclee");
		assertEquals(c.isHidden(), false);
		assertEquals(c.getCreateTime(), CalendarParser.toCalendar("2009-01-01 01:10:54"));
		assertEquals(c.getUpdateTime(), CalendarParser.toCalendar("2009-01-01 01:10:55"));
		assertEquals(c.getAlias(), "bea2");
		assertEquals(c.getCity(), "nyork");
		assertEquals(c.getAddress(), "the whitehouse");
		assertEquals(c.getTaxNo(), "234567800");
		assertEquals(c.getFoundTime(), CalendarParser.toCalendar("2009-01-10 01:09:56"));
		assertEquals(c.getScale(), "20-50");
		assertEquals(c.getIndustry(), "IT");
	}
	
	public void testDelete() {
		testInsert();
		Company c = (Company)Mapper.getInstance().queryForObject("SelectCompanyById", "1234098834567654uio");
		Mapper.getInstance().delete("DeleteCompany", c);
		c = (Company)Mapper.getInstance().queryForObject("SelectCompanyById", "1234098834567654uio");
		assertEquals(c, null);
	}
	
	public void testParent() {
		//create company
		testInsert();
		
		//get this company as party
		Party p1 = (Party)Mapper.getInstance().queryForObject("SelectPartyById", "1234098834567654uio");
		
		//create organization
		Organization org = new Organization();
		org.setId("sdfghthhgbyugbuyhj");
		org.setCompany((Company)p1);
		org = (Organization)Mapper.getInstance().insert("InsertOrganization", org);
		
		//add organization into company
		org.setParent(p1);
		Party parent = org.getParent();
		assertEquals(p1.getId(), parent.getId());
		List<Party> children = p1.getChildren();
		assertEquals(children.size(), 1);
		assertEquals(children.get(0).getId(), "sdfghthhgbyugbuyhj");
		
		//create person
		Person person = new Person();
		person.setId("iopgjhtyhvgjhtuhj3435467654");
		person.setCompany((Company)p1);
		person = (Person)Mapper.getInstance().insert("InsertPerson", person);
		
		//add person into organization
		person.setParent(org);
		parent = person.getParent();
		assertEquals(parent.getId(), "sdfghthhgbyugbuyhj");
		children = p1.getAllChildren();
		assertEquals(children.size(), 2);
		
		//create another person
		Person person2 = new Person();
		person2.setId("3cy7uhvhjiopp;jhiokjh..");
		person2.setCompany((Company)p1);
		person2 = (Person)Mapper.getInstance().insert("InsertPerson", person2);
		
		//create role
		Role role = new Role();
		role.setId("tyuikjniuytfvbuewqazxtrtyu");
		role.setCompany((Company)p1);
		role = (Role)Mapper.getInstance().insert("InsertRole", role);
		
		//add role into company
		p1.addChild(role);
		assertEquals(p1.getChildren().size(), 2);
		assertEquals(p1.getAllChildren().size(), 3);
		
		//add person into role
		role.addChild(person2, org);
		assertEquals(p1.getChildren().size(), 2);
		assertEquals(p1.getAllChildren().size(), 4);
	}
}
